new Vue({
    el:"#app",
    data(){
        return{
            msg:null,
            adddate:{
                id:"",
                name:"",
                age:"",
                height:"",
                school:""
            },
            show:false,
            show1:true,
            index1:""
        }
    },
    created(){
     this.init1();
    },

    methods:{
        init1(){
            axios
                .get('/web_war_exploded/showStu')
                .then(response =>(this.msg=response.data))
                .catch(function (error){
                    console.log(error);
                });

        },

      del:function (index,id){
          if(confirm("确定删除吗？")){
              alert(id)
              this.msg.splice(index,1)

              axios.get('/web_war_exploded/vuedel?data='+id)
                  .then(function(response){
                      if (response.data=='success') {
                          this.init1();
                      }
                  }).catch(function (error){
                  console.log(error);
              });


          }
      },
      //数据回显
      update:function(index){
          this.show=true;
          this.show1=false;

          let data1={};
          data1={...this.msg[index]};
          this.adddate=data1;
          this.index1=index;
      } ,
      //实现更新渲染
      updateok:function(){
            let me=this;
          this.show=false;
          this.show1=true;
          let data3=encodeURIComponent(JSON.stringify(this.adddate));
          axios.get('/web_war_exploded/vueupdate?data='+data3,)
              .then(function(response){
                  if (response.data=='success') {
                     me.adddate={};
                     me.init1();
                  }
              }).catch(function (error){
              console.log(error);
          });

      },
        //添加
      add:function(){
          if(this.adddate.name=="" || this.adddate.age=="" || this.adddate.height==""){
              alert("数据不完整")
              return;
          }
          let data={};//中间值
          data={...this.adddate}
          //深拷贝浅拷贝
          this.msg.push(data);
          this.adddate.id=""
          this.adddate.name=""
          this.adddate.age=""
          this.adddate.height=""
          this.adddate.school=""
          //序列化
          let data1=encodeURIComponent(JSON.stringify(data));
          axios.get('/web_war_exploded/vueinsert?data='+data1,)
              .then(function(response){
                  if (response.data=='success') {
                      this.init1();
                  }
              }).catch(function (error){
                  console.log(error);
              });
        }
    }
}) 